/**********************************************************************************************/
// dgt_Utils.cpp
//
// Some util functions for dgtKernel
// Copyright Diamant Group 2008
/**********************************************************************************************/
#include "dgtKernel.h"
#include "dgt_Utils.h"

using namespace std;

/**********************************************************************************************/
bool get_Bit( unsigned inNum, unsigned inBitNumber )
{
	struct t_FourByte 
	{ 
		unsigned b1:1;
		unsigned b2:1;
		unsigned b3:1;
		unsigned b4:1;
		unsigned b5:1;
		unsigned b6:1;
		unsigned b7:1;
		unsigned b8:1;
		unsigned b9:1;
		unsigned b10:1;
		unsigned b11:1;
		unsigned b12:1;
		unsigned b13:1;
		unsigned b14:1;
		unsigned b15:1;
		unsigned b16:1;
		unsigned b17:1;
		unsigned b18:1;
		unsigned b19:1;
		unsigned b20:1;
		unsigned b21:1;
		unsigned b22:1;
		unsigned b23:1;
		unsigned b24:1;
		unsigned b25:1;
		unsigned b26:1;
		unsigned b27:1;
		unsigned b28:1;
		unsigned b29:1;
		unsigned b30:1;
		unsigned b31:1;
		unsigned b32:1;
	};

	union bits 
	{
		unsigned c;
		struct t_FourByte bit;
	} n;

	n.c = inNum;

	switch( inBitNumber )
	{
		case 1: return n.bit.b1;
		case 2: return n.bit.b2;
		case 3: return n.bit.b3;
		case 4: return n.bit.b4;
		case 5: return n.bit.b5;
		case 6: return n.bit.b6;
		case 7: return n.bit.b7;
		case 8: return n.bit.b8;
		case 9: return n.bit.b9;
		case 10: return n.bit.b10;
		case 11: return n.bit.b11;
		case 12: return n.bit.b12;
		case 13: return n.bit.b13;
		case 14: return n.bit.b14;
		case 15: return n.bit.b15;
		case 16: return n.bit.b16;
		case 17: return n.bit.b17;
		case 18: return n.bit.b18;
		case 19: return n.bit.b19;
		case 20: return n.bit.b20;
		case 21: return n.bit.b21;
		case 22: return n.bit.b22;
		case 23: return n.bit.b23;
		case 24: return n.bit.b24;
		case 25: return n.bit.b25;
		case 26: return n.bit.b26;
		case 27: return n.bit.b27;
		case 28: return n.bit.b28;
		case 29: return n.bit.b29;
		case 30: return n.bit.b30;
		case 31: return n.bit.b31;
		case 32: return n.bit.b32;
	}
}

/**********************************************************************************************/
unsigned		get_NumFromBits( wxBoolArray inArray )
{
	unsigned result = 0;
	for( unsigned i = 0; i < inArray.Count(); i++ )
	{
		bool tItem = inArray.Item( i );
		result+=( pow( (double) 2, (double) i ) * tItem );
	}
	return result; 
}

/**********************************************************************************************/
pair<wxString, unsigned>	get_LibAndNum( dgt_BoolElement_Ptr inElement )
{
	pair<wxString, unsigned> result;

	if( inElement->get_Operation() == AND )
	{
		result.first = _("log_and.dll");
		unsigned t_inpCount = inElement->Count();
		if( t_inpCount == 2 )
			result.second = 0;	
		else if( t_inpCount == 3 )
			result.second = 1;
		else if( t_inpCount == 4 )
			result.second = 2;
		else if( t_inpCount == 5 )
			result.second = 3;
	}
	if( inElement->get_Operation() == OR )
	{
		result.first = _("log_or.dll");
		unsigned t_inpCount = inElement->Count();
		if( t_inpCount == 2 )
			result.second = 0;	
		else if( t_inpCount == 3 )
			result.second = 1;
		else if( t_inpCount == 4 )
			result.second = 2;
		else if( t_inpCount == 5 )
			result.second = 3;
	}

	return result;
}